package com.myaliyun.oss;

import com.aliyun.oss.OSSClient;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

/**
 *
 * 上传数据之阿里云oss工具类
 */
//@Component
@Data
public class AliOSSUtils {

    public static final Logger LOGGER = LoggerFactory.getLogger(AliOSSUtils.class);

    /**
     * 生成访问图片地址有效期
     */
    private static final int VISIT_URL_EXPIRATION = 365*10;


    private AliOSSProperties aliOSSProperties;




    /**
     * uploadFile:上传文件到Oss
     * @param accesKeyId accesKeyId
     * @param accessKeySecret accessKeySecret
     * @param endpoint endpoint
     * @param savePath 存放路径
     * @param bucketName bucket名字
     * @param imageName 图片名字
     * @param fileInputStream 图片流
     * @param fileSize fileSize 图片大小
     * @throws Exception
     */
//    public static void uploadFile(String accesKeyId, String accessKeySecret, String endpoint, String savePath, String bucketName,
//                                  String imageName, InputStream fileInputStream, Long fileSize) throws Exception {
//        OSSClient client = new OSSClient(endpoint, accesKeyId, accessKeySecret);
//        try {
//            ObjectMetadata objectMeta = new ObjectMetadata();
//            objectMeta.setContentLength(fileSize);
//            if (!savePath.endsWith("/")) {
//                savePath = savePath + "/";
//            }
//            client.putObject(bucketName,savePath + imageName, fileInputStream, objectMeta);
//        } catch (Exception e) {
//            LOGGER.error("上传文件到oss出错", e);
//            throw new Exception("上传文件到oss出错");
//        } finally {
//            if (fileInputStream != null) {
//                try {
//                    fileInputStream.close();
//                    client.shutdown();
//                } catch (IOException e) {
//                    LOGGER.error("上传文件到oss出错", e);
//                }
//            }
//        }
//    }
    public  void uploadFile(String imageName, InputStream fileInputStream) throws Exception {
        OSSClient client = new OSSClient(aliOSSProperties.getEndpoint(), aliOSSProperties.getAccesKeyId(), aliOSSProperties.getAccessKeySecret());
        try {
//            ObjectMetadata objectMeta = new ObjectMetadata();
//            objectMeta.setContentLength(fileSize);
//            if (!savePath.endsWith("/")) {
//                savePath = savePath + "/";
//            }
            client.putObject(aliOSSProperties.getBucketName(),imageName, fileInputStream);
        } catch (Exception e) {
            LOGGER.error("上传文件到oss出错", e);
            throw new Exception("上传文件到oss出错");
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                    client.shutdown();
                } catch (IOException e) {
                    LOGGER.error("上传文件到oss出错", e);
                }
            }
        }
    }









    /**
     * 生成原图访问地址
     * @param ossAccessKeyId
     * @param ossAccessKeySecret
     * @param ossEndPoint
     * @param ossBucketName
     * @param imageName
     * @param path
     * @return
     * @throws Exception
     */
//    public static String getVisitUrl( String ossAccessKeyId , String ossAccessKeySecret, String ossEndPoint, String ossBucketName, String path, String imageName ) throws Exception{
//        OSSClient client = new OSSClient(ossEndPoint, ossAccessKeyId, ossAccessKeySecret);
//        try {
//            Calendar calendar = Calendar.getInstance();
//            calendar.add(Calendar.DAY_OF_YEAR, VISIT_URL_EXPIRATION);
//            return client.generatePresignedUrl(ossBucketName, path + "/" + imageName, calendar.getTime()).toString();
//        } catch (Exception e) {
//            throw new Exception("生成访问地址出错");
//        }finally {
//            client.shutdown();
//        }
//    }
    public  String getVisitUrl(String imageName ) throws Exception{
        OSSClient client = new OSSClient(aliOSSProperties.getEndpoint(), aliOSSProperties.getAccesKeyId(), aliOSSProperties.getAccessKeySecret());
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, VISIT_URL_EXPIRATION);
            return client.generatePresignedUrl(aliOSSProperties.getBucketName(),  imageName, calendar.getTime()).toString();
        } catch (Exception e) {
            throw new Exception("生成访问地址出错");
        }finally {
            client.shutdown();
        }
    }



    /**
     * 删除Oss源文件
     * @param ossAccessKeyId
     * @param ossAccessKeySecret
     * @param ossEndPoint
     * @param ossBucketName
     * @param savePath
     * @param bucketName
     * @param imageName
     */
    public  void delete(String accesKeyId,String accessKeySecret,String endpoint,String savePath,String bucketName,
                              String imageName) throws Exception {
        OSSClient client = new OSSClient(aliOSSProperties.getEndpoint(), aliOSSProperties.getAccesKeyId(), aliOSSProperties.getAccessKeySecret());
        try {
            client.deleteObject(aliOSSProperties.getBucketName(),  imageName);
        } catch (Exception e) {
            LOGGER.error("删除oss文件出错", e);
            throw new Exception("删除oss文件出错");
        } finally {
            client.shutdown();
        }
    }

}